$(function () {
    $("#department_datagrid").datagrid({
        fit:true,
        url:'/department/list',
        rownumbers:true,
        singleSelect:true,
        pagination:true,
        checkOnSelect:true,
        fitColumns:true,
        toolbar:department_datagrid_Btn,
        columns:[
            [
            {field:'sn',title:'部门编号',align:'center',width:10},
            {field:'name',title:'部门名称',align:'center',width:10},
            ]
        ]
    })
    $("#department_dialog").dialog({
        title:'新增员工',
        width:350,
        height:300,
        buttons:'#department_dialog_bt',
        closed: true
    })
    $("#searchBtn").textbox({
        onClickButton:function () {
           var keyword =  $("#searchBtn").val();
            $("#department_datagrid").datagrid("load",{
                keyword:keyword
            })
        }
    })
})
function stateFormatter(value){
    if(value ==1){
        return "<font color='#adff2f'>是</font>"
    }else{
        return "<font color='red'>否</font>"
    }
}

function department_add(){
    $("#department_dialog").dialog("open")
    $("#department_form").form("clear")
}
function deptFormatter(value) {
    return value.name;
}
//数据编辑===================================================================
function department_edit() {
//表格单行选择==============================================================================
    var rowData = $("#department_datagrid").datagrid("getSelected");
//判断单行选择的数据==============================================================================
    if(rowData){
        /*按钮不同但是窗口还是要打开的*/
        $("#department_dialog").dialog("open");

//提交表单=====================================================================================
        $("#department_form").form("load",rowData);
    }else{
        $.messager.alert("温馨提示","请选择需要编辑的数据","warning");
    }
}
function department_cancel() {
    //进行取消窗口页面
    $("#department_dialog").dialog("close")
}
function department_reload() {
    //进行刷新表单
    $("#department_datagrid").datagrid("reload");
}

//添加==================================================================
function department_save() {
     var url;
        var idVal = $("[name=id]").val();
    if(idVal){
        //进行判断
        url = "/department/update"
    }else{
        url ="/department/save"
    }
    //在这里进行使用submit方法就是进行提交表单了
    $("#department_form").form("submit",{
     url:url,
        success:function (data) {
           data=  $.parseJSON(data);
           if(data.success){
               $.messager.alert("温馨提示",data.msg,"info",function () {
                   //刷新数据表格
                   $("#department_datagrid").datagrid("reload");
                   //关闭对话框
                   $("#department_dialog").dialog("close")
               });
               }
               else {
               $.messager.alert("温馨提示",data.msg,"error")
           }
        }
    });
}
//删除==================================================================
function department_remove(){
    var rowData = $("#department_datagrid").datagrid("getSelected");
    if(rowData){
        $.messager.confirm('温馨提示','您确认想要删除记录吗？',function(yes){
            if(yes){
                $.post("/department/delete?id="+rowData.id,function (data) {
                    if(data.success){
                        $.messager.alert("温馨提示",data.msg,"info",function () {
                            $("#department_datagrid").datagrid("reload");
                        });
                    }else {
                        $.messager.alert("温馨提示",data.msg,"error");
                    }
                },"json");
            }
        });
    }else{
        $.messager.alert("温馨提示","请选择需要删除的数据","warning");
    }
}